package com.nuc.thread.cyclicbarrier;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 诗酒趁年华
 * @version 1.0.0
 * @Description
 * @createTime 2021年08月15日 17:28:00
 */
@Slf4j
public class TestCycliBarrier {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2,()->{
            log.debug("task1,task finished..");
        });

        ExecutorService pool = Executors.newFixedThreadPool(2);
        pool.submit(()->{
            log.debug("task1 begin...");
            try {
                Thread.sleep(1000);
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        });
        pool.submit(()->{
            log.debug("task2 begin...");
            try {
                Thread.sleep(1000);
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        });


    }
}
